package com.gsit.ocraspect.controller;

import java.text.DateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.Errors;
import org.springframework.web.HttpSessionRequiredException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.gsit.ocrbatch.domain.Login;
import com.gsit.ocrbatch.domain.Record;
import com.gsit.ocrbatch.domain.WebConfig;
import com.gsit.ocrbatch.services.RecordService;
import com.gsit.ocrbatch.utils.Constant;
@Controller
@SessionAttributes(Constant.LOGIN)
public class LoginController {
	
	private static final Logger logger = LoggerFactory.getLogger(LoginController.class);
	@Autowired
	private RecordService recordService; 
	
	@Autowired
	private WebConfig webConfig;

	
	@RequestMapping(value = "/", method = RequestMethod.GET)
	public String start(Locale locale) {
		return "redirect:/index";
	}
	
	@RequestMapping(value = "/index", method = RequestMethod.GET)
	public String index(Locale locale, Model model) {
		model.addAttribute(Constant.LOGIN, new Login() );
		return "index";
	}
	
	@RequestMapping(value = "/logout", method = RequestMethod.GET)
	public String logout(Locale locale, final HttpSession httpSession) {
		httpSession.invalidate();
		return "redirect:/";
	}
	
	@RequestMapping(value = "/login", method = RequestMethod.POST)
	public String login(Locale locale,@ModelAttribute(Constant.LOGIN) Login login,Model model,final Errors errors, final HttpSession httpSession) {
		String path = "index";
		if(!isValidLogin(login, errors)){
			model.addAttribute(Constant.LOGIN, login );
			path= "index";
		}else if(login.getUsername().equals("admin")&&login.getPassword().equals("admin")){
			logger.info("Welcome home! the client locale is "+ locale.toString());
			
			List<Record> records =recordService.findByAllWaitingRecords();
			
			Date date = new Date();
			DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
			String formattedDate = dateFormat.format(date);
			model.addAttribute("serverTime", formattedDate );
			model.addAttribute("records", records );
			
			httpSession.setAttribute(Constant.LOGIN, login);
			httpSession.setAttribute(Constant.WEBCONFIG, webConfig);
			
			path="home";
		}
		
		return path;
	}
	
	private Boolean isValidLogin(Login login,final Errors errors){
		
		Boolean result = Boolean.TRUE;
		
		if(StringUtils.isBlank(login.getUsername())){
			errors.rejectValue("username", "required","User name is required");
			result = Boolean.FALSE;
		}
		if(StringUtils.isBlank(login.getPassword())){
			errors.rejectValue("password", "required", "Password is required");
			result = Boolean.FALSE;
		}
		
		return result;
	}
	
	
}
